<template>
  <div style="height: 600px; background-color: #f6f7f9">
    <!-- 顶部导航 -->
    <van-sticky>
      <van-nav-bar title="修改地址" left-arrow @click-left="$router.go(-1)">
      </van-nav-bar>
    </van-sticky>
    <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        add-button-text="提交修改"
        @add="onUpdate"
        @select="choseAddress"
    />
  </div>
</template>

<script>
export default {
  name: "UpdateAddress",
  data() {
    return {
      chosenAddressId: '1',
      choseAddressInfo: {
        name: '张三',
        tel: '13000000000',
        address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
      },
      list: [
        {
          id: '1',
          name: '张三',
          tel: '13000000000',
          address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
          isDefault: true,
        },
      ],
    };
  },
  methods: {
    showAddress(){
      console.log('在localStorage中的JWT数据：' + localStorage.getItem('jwt'));
      let url = 'http://localhost:9080/addresses';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        console.log(response.data.data);
        let responseBody=response.data.data;
        this.list = responseBody;
      })
    },
    choseAddress(item){
     this.choseAddressInfo=item;
      console.log("当前中的地址信息->>>>",this.choseAddressInfo)
    },
    onUpdate(){
      let orderId = localStorage.getItem('orderId');
      let url = 'http://localhost:9080/orders/'+orderId+'/update';
      let choseAddressInfo = this.qs.stringify(this.choseAddressInfo, {arrayFormat: 'repeat'});
      console.log('choseAddressInfo = ' + choseAddressInfo);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, choseAddressInfo).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.state == 20000) {
          this.$toast.success('修改地址成功!');
          this.$router.go(-1);
        }else{
          this.$toast.fail(responseBody.message);
        }
      });
    }
  },
  mounted() {
    this.showAddress();
  }
}
</script>

<style scoped>

</style>
